package com.quanquan.management.entity;

import com.quanquan.management.core.utils.StringUtils;
import lombok.Data;
import org.apache.shiro.authz.Permission;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @description:
 * @author:lihonglinag
 * @date:2019-03-21 11:28
 * @version:v 1.0.0
 */
@Data
@Entity
@Table(name = "oo_sys_permission")
public class SysPermission implements Permission,Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(name = "id", nullable = false, length = 50)
    private String id;

    @Column(name = "name", nullable = false, length = 50)
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "url")
    private String url;

    @Column(name = "parent_id")
    private String parentId;

    @Column(name = "reg_time", nullable = false)
    private Date regTime;

    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    @Override
    public boolean implies(Permission permission) {
        if (!(permission instanceof SysPermission)) {
            return false;
        }
        SysPermission up = (SysPermission) permission;
        if ("*".equals(up.getUrl())) {
            return true;
        }
        if (StringUtils.isEmpty(up.getUrl())) {
            return false;
        }
        if (this.getUrl().equals(up.getUrl())) {
            return true;
        }
        return false;
    }
}
